package controller;

import manager.BidMgr;
import manager.SectionStudentMgr;
import manager.StudentMgr;
import controller.validator.SectionStudentValidator;
import entity.Section;
import entity.Student;
import exception.InputValidationErrors;

/**
 * @author huiwensong.2012, huangjm.2012, sheryl.tay.2012, mohameds.2012
 * 
 */

public class SectionStudentCtrl {

	private SectionStudentMgr sectionStudentMgr;
	private BidMgr bidMgr;
	private StudentMgr studentMgr = new StudentMgr();

	public SectionStudentCtrl() {
		sectionStudentMgr = new SectionStudentMgr();
		bidMgr = new BidMgr();
	}

	public void addSectionForStudent(Section section, Student student) {
		sectionStudentMgr
				.addSectionEnrollmentForStudent(section.getCourse()
						.getCourseCode(), section.getSectionCode(), student
						.getUserId());
	}

	public boolean dropSectionForStudent(String courseCode, String sectionCode,
			String userId) throws InputValidationErrors {

		SectionStudentValidator sectionStudentValidator = new SectionStudentValidator(
				courseCode, sectionCode, userId);
		sectionStudentValidator.performValidationBeforeDeleting();

		boolean isDropped = sectionStudentMgr
				.deleteSectionEnrollmentForStudent(courseCode, sectionCode,
						userId);

		// also delete from pending bid
		bidMgr.deleteBid(userId, courseCode, sectionCode);

		// refund edollar
		studentMgr.refundEdollarForStudent(sectionStudentValidator.getBid()
				.getBidAmount(), userId);

		return isDropped;
	}

	/**
	 * Computes vacancy for section. Vacancy = section size - num of successful
	 * bids.
	 * 
	 * @param section
	 * @return
	 */
	public int computeVacancyForSection(Section section) {
		int numSuccesfulBids = sectionStudentMgr
				.retrieveNumSuccessfulBidsForSection(section.getCourse()
						.getCourseCode(), section.getSectionCode());
		return section.getSize() - numSuccesfulBids;
	}
}
